import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _startsWith from "lodash/startsWith";
import _set from "lodash/set";
import _map from "lodash/map";
import _isNumber from "lodash/isNumber";
import _forEach from "lodash/forEach";
import _cloneDeep from "lodash/cloneDeep";
import { hiddenColumnPrefix } from 'components/DynamicComponent/DynamicTable/utils';
import { defaultFormCol, emptyField } from "./config";
export function processPageConfig() {
  var pageConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

  var newPageConfig = _cloneDeep(pageConfig); // config 的 fields 需要分别处理


  var tpls = newPageConfig.fields || [];
  var processedTpls = [];

  for (var tplIndex = 0; tplIndex < tpls.length; tplIndex += 1) {
    var tpl = tpls[tplIndex] || {};
    var processedTpl = void 0;

    switch (tpl.templateType) {
      case 'DynamicForm':
        processedTpl = processDynamicForm(tpl);
        break;

      case 'DynamicToolbar':
        processedTpl = processDynamicToolbar(tpl);
        break;

      case 'DynamicTable':
        processedTpl = processDynamicTable(tpl);
        break;

      default:
        processedTpl = tpl;
        break;
    }

    processedTpls.push(processedTpl);
  }

  newPageConfig.fields = processedTpls;
  return newPageConfig;
}
/**
 * 会直接改变参数
 * @param {object} dynamicForm
 * @returns {object} dynamicForm
 */

function processDynamicForm(dynamicForm) {
  // 认为组件的 col 和 组件字段的 leftOffset 与 rightOffset 对得上
  var newFields = []; // 处理组件的 属性

  var formProps = {};

  _forEach(dynamicForm.config, function (prop) {
    formProps[prop.attributeName] = prop.value;
  });

  var col = formProps.col || defaultFormCol;
  var walkerCount = 0;
  var row = [];

  _forEach(dynamicForm.fields, function (field) {
    var fieldWalkerCount = 0;

    if (_isNumber(field.leftOffset)) {
      while (fieldWalkerCount < field.leftOffset) {
        row.push(_cloneDeep(emptyField));
        fieldWalkerCount += 1;
      }
    }

    walkerCount += fieldWalkerCount;
    fieldWalkerCount = 0;
    row.push(field);

    if (_isNumber(field.colspan) && field.colspan) {
      walkerCount += field.colspan;
    } else {
      walkerCount += 1;
    }

    if (_isNumber(field.rightOffset)) {
      while (fieldWalkerCount < field.rightOffset) {
        row.push(_cloneDeep(emptyField));
        fieldWalkerCount += 1;
      }
    }

    walkerCount += fieldWalkerCount;

    if (walkerCount >= col) {
      newFields.push(row);
      row = [];
      walkerCount = 0;
    }
  }); // 也许数据库中出现问题了，leftOffset rightOffset 和字段对不上
  // if(row.length > 0){
  //   newFields.push(row);
  // }


  if (newFields.length === 0) {
    for (var index = 0; index < col; index += 1) {
      row.push(_cloneDeep(emptyField));
    }

    newFields.push(row);
  }

  return _objectSpread(_objectSpread({}, dynamicForm), {}, {
    fields: newFields
  });
}
/**
 * 会直接改变参数
 * @param {object} dynamicToolbar
 * @returns {object} dynamicToolbar
 */


function processDynamicToolbar(dynamicToolbar) {
  dynamicToolbar.fields = _map(dynamicToolbar.fields, function (field) {
    var props = {};

    _forEach(field.config, function (prop) {
      props[prop.attributeName] = prop.value;
    });

    return _objectSpread(_objectSpread({}, field), {}, {
      type: props.type,
      style: {
        marginRight: props['style.marginRight']
      }
    });
  });
  return dynamicToolbar;
}
/**
 * 会直接改变参数
 * @param {object} dynamicTable
 * @returns {object} dynamicTable
 */


function processDynamicTable(dynamicTable) {
  var props = {};
  var hiddenColumns = [];
  var newConfig = [];

  _forEach(dynamicTable.config, function (prop) {
    if (_startsWith(prop.attributeName, hiddenColumnPrefix)) {
      hiddenColumns.push(prop);
    } else {
      newConfig.push(prop);
      props[prop.attributeName] = prop.value;
    }
  });

  dynamicTable.config = newConfig;
  dynamicTable.hiddenColumns = hiddenColumns;
  dynamicTable.fields = _map(dynamicTable.fields, function (field) {
    switch (field.componentType) {
      case 'LinkButton':
        return processDynamicTableLinkButtonField(field, dynamicTable);

      default:
        return field;
    }
  });
  dynamicTable.pagination = props.pagination;

  if (props.pagination) {
    dynamicTable.defaultPageSize = props.defaultPageSize;
  }

  return dynamicTable;
}
/**
 * 处理 table 行按钮
 * @param {object} linkButton
 // * @param {object} dynamicTable
 * @returns {object}
 */


function processDynamicTableLinkButtonField(linkButton) {
  // todo 确定 DynamicTable 的 linkButton 没有其他的属性值, 且只有一级属性
  var props = {};
  var btns = [];

  _forEach(linkButton.config, function (attr) {
    _set(props, attr.attributeName, attr);
  });

  Object.keys(props).forEach(function (btnKey) {
    var btnProps = props[btnKey];
    var btnConfig = [];
    var orderSeq;
    Object.keys(btnProps).forEach(function (btnPropKey) {
      switch (btnPropKey) {
        case 'orderSeq':
          orderSeq = btnProps[btnPropKey].value;
          break;

        default:
          break;
      } // 将对应的 attr 放进按钮中


      btnProps[btnPropKey].attributeName = btnPropKey;
      btnConfig.push(btnProps[btnPropKey]);
    });
    btns[orderSeq] = btnConfig; // btns.push(btnConfig);
  });
  linkButton.btns = btns;
  return linkButton;
}
/**
 * 获取 字段组件的属性
 * @param component
 */


export function getFieldProps(component) {
  var fieldProps = {};

  if (component.enabledFlag !== 1) {
    fieldProps.disabled = true;
  }

  switch (component.componentType) {
    case 'DatePicker':
      fieldProps.placeholder = '';
      break;

    default:
      break;
  }

  return fieldProps;
}